home *** CD-ROM | disk | FTP | other *** search
/ Freelog 115 / FreelogNo115-MaiJuin2013.iso / Internet / Filezilla Server / FileZilla_Server-0_9_41.exe / source / interface / OptionsSslPage.cpp < prev    next >
C/C++ Source or Header  |  2011-11-06  |  7KB  |  241 lines

  1. // FileZilla Server - a Windows ftp server
  2.  
  3. // Copyright (C) 2002-2004 - Tim Kosse <tim.kosse@gmx.de>
  4.  
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the GNU General Public License
  7. // as published by the Free Software Foundation; either version 2
  8. // of the License, or (at your option) any later version.
  9.  
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. // GNU General Public License for more details.
  14.  
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program; if not, write to the Free Software
  17. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  18.  
  19. #include "stdafx.h"
  20. #include "filezilla server.h"
  21. #include "OptionsDlg.h"
  22. #include "OptionsPage.h"
  23. #include "OptionsSslPage.h"
  24. #include "GenerateCertificateDlg.h"
  25. #include "../AsyncSslSocketLayer.h"
  26. #include <set>
  27.  
  28. COptionsSslPage::COptionsSslPage(COptionsDlg *pOptionsDlg, CWnd* pParent /*=NULL*/)
  29.     : COptionsPage(pOptionsDlg, COptionsSslPage::IDD, pParent)
  30.     , m_enabled(FALSE)
  31.     , m_allowExplicit(FALSE)
  32.     , m_certificate(_T(""))
  33.     , m_sslports(_T(""))
  34. {
  35. }
  36.  
  37. COptionsSslPage::~COptionsSslPage()
  38. {
  39. }
  40.  
  41. void COptionsSslPage::DoDataExchange(CDataExchange* pDX)
  42. {
  43.     COptionsPage::DoDataExchange(pDX);
  44.     DDX_Check(pDX, IDC_ENABLESSL, m_enabled);
  45.     DDX_Check(pDX, IDC_ALLOWEXPLICIT, m_allowExplicit);
  46.     DDX_Check(pDX, IDC_FORCEEXPLICIT, m_forceExplicit);
  47.     DDX_Check(pDX, IDC_FORCEPROTP, m_forceProtP);
  48.     DDX_Text(pDX, IDC_CERTIFICATE, m_certificate);
  49.     DDX_Text(pDX, IDC_PRIVATEKEY, m_key);
  50.     DDX_Text(pDX, IDC_KEYPASS, m_pass);
  51.     DDX_Text(pDX, IDC_SSLONLY, m_sslports);
  52.     DDX_Control(pDX, IDC_ALLOWEXPLICIT, m_cAllowExplicit);
  53.     DDX_Control(pDX, IDC_FORCEEXPLICIT, m_cForceExplicit);
  54.     DDX_Control(pDX, IDC_FORCEPROTP, m_cForceProtP);
  55.     DDX_Control(pDX, IDC_CERTIFICATE, m_cCertificate);
  56.     DDX_Control(pDX, IDC_CERTIFICATE_BROWSE, m_cCertificateBrowse);
  57.     DDX_Control(pDX, IDC_PRIVATEKEY, m_cKey);
  58.     DDX_Control(pDX, IDC_PRIVATEKEY_BROWSE, m_cKeyBrowse);
  59.     DDX_Control(pDX, IDC_SSLONLY, m_cSslports);
  60.     DDX_Control(pDX, IDC_KEYPASS, m_cPass);
  61. }
  62.  
  63. BEGIN_MESSAGE_MAP(COptionsSslPage, COptionsPage)
  64.     ON_BN_CLICKED(IDC_GENERATE, OnGenerate)
  65.     ON_BN_CLICKED(IDC_PRIVATEKEY_BROWSE, OnKeyBrowse)
  66.     ON_BN_CLICKED(IDC_CERTIFICATE_BROWSE, OnCertificateBrowse)
  67.     ON_BN_CLICKED(IDC_ENABLESSL, OnEnableSsl)
  68.     ON_BN_CLICKED(IDC_ALLOWEXPLICIT, OnEnableSsl)
  69. END_MESSAGE_MAP()
  70.  
  71. void COptionsSslPage::OnGenerate()
  72. {
  73.     UpdateData();
  74.     CGenerateCertificateDlg dlg;
  75.     if (dlg.DoModal() == IDOK)
  76.     {
  77.         m_key = dlg.m_file;
  78.         m_certificate = dlg.m_file;
  79.         UpdateData(FALSE);
  80.     }
  81. }
  82.  
  83. void COptionsSslPage::OnKeyBrowse()
  84. {
  85.     UpdateData();
  86.     CFileDialog dlg(TRUE);
  87.     if (dlg.DoModal() == IDOK)
  88.     {
  89.         m_key = dlg.GetPathName();
  90.         UpdateData(FALSE);
  91.     }
  92. }
  93.  
  94. void COptionsSslPage::OnCertificateBrowse()
  95. {
  96.     UpdateData();
  97.     CFileDialog dlg(TRUE);
  98.     if (dlg.DoModal() == IDOK)
  99.     {
  100.         m_certificate = dlg.GetPathName();
  101.         UpdateData(FALSE);
  102.     }
  103. }
  104.  
  105. void COptionsSslPage::OnEnableSsl()
  106. {
  107.     UpdateData();
  108.     m_cAllowExplicit.EnableWindow(m_enabled);
  109.     m_cCertificate.EnableWindow(m_enabled);
  110.     m_cCertificateBrowse.EnableWindow(m_enabled && m_pOptionsDlg->IsLocalConnection());
  111.     m_cKey.EnableWindow(m_enabled);
  112.     m_cKeyBrowse.EnableWindow(m_enabled && m_pOptionsDlg->IsLocalConnection());
  113.     m_cSslports.EnableWindow(m_enabled);
  114.     m_cForceExplicit.EnableWindow(m_enabled && m_allowExplicit);
  115.     m_cForceProtP.EnableWindow(m_enabled);
  116.     m_cPass.EnableWindow(m_enabled);
  117. }
  118.  
  119. BOOL COptionsSslPage::IsDataValid()
  120. {
  121.     USES_CONVERSION;
  122.  
  123.     UpdateData();
  124.  
  125.     std::set<int> portSet;
  126.     bool valid = true;
  127.  
  128.     CString ports = m_sslports;
  129.     ports.TrimLeft(_T(" ,"));
  130.  
  131.     int pos = ports.FindOneOf(_T(" ,"));
  132.     while (pos != -1 && valid)
  133.     {
  134.         int port = _ttoi(ports.Left(pos));
  135.         if (port < 1 || port > 65535)
  136.         {
  137.             valid = false;
  138.             break;
  139.         }
  140.         else
  141.             portSet.insert(port);
  142.         ports = ports.Mid(pos + 1);
  143.         ports.TrimLeft(_T(" ,"));
  144.         pos = ports.FindOneOf(_T(" ,"));
  145.     }
  146.     if (valid && ports != _T(""))
  147.     {
  148.         int port = _ttoi(ports);
  149.         if (port < 1 || port > 65535)
  150.             valid = false;
  151.         else
  152.             portSet.insert(port);
  153.     }
  154.  
  155.     if (!valid && m_enabled)
  156.     {
  157.         m_pOptionsDlg->ShowPage(this);
  158.         m_cSslports.SetFocus();
  159.         AfxMessageBox(_T("Invalid port found, please only enter ports in the range from 1 to 65535."));
  160.         return FALSE;
  161.     }
  162.  
  163.     m_sslports = _T("");
  164.     for (std::set<int>::const_iterator iter = portSet.begin(); iter != portSet.end(); iter++)
  165.     {
  166.         CString tmp;
  167.         tmp.Format(_T("%d "), *iter);
  168.         m_sslports += tmp;
  169.     }
  170.     m_sslports.TrimRight(' ');
  171.     UpdateData(false);
  172.  
  173.     if (m_enabled && m_pOptionsDlg->IsLocalConnection())
  174.     {
  175.         CAsyncSslSocketLayer layer;
  176.         CString error;
  177.         int res = layer.SetCertKeyFile(T2A(m_certificate), T2A(m_key), T2A(m_pass), &error);
  178.         if (res == SSL_FAILURE_LOADDLLS)
  179.         {
  180.             m_pOptionsDlg->ShowPage(this);
  181.             AfxMessageBox(_T("Failed to load SSL libraries"));
  182.             return FALSE;
  183.         }
  184.         else if (res == SSL_FAILURE_INITSSL)
  185.         {
  186.             m_pOptionsDlg->ShowPage(this);
  187.             AfxMessageBox(_T("Failed to initialize SSL libraries"));
  188.             return FALSE;
  189.         }
  190.         else if (res == SSL_FAILURE_VERIFYCERT)
  191.         {
  192.             m_pOptionsDlg->ShowPage(this);
  193.             if (error != _T(""))
  194.                 AfxMessageBox(error);
  195.             else
  196.                 AfxMessageBox(_T("Failed to set certificate and private key"));
  197.             return FALSE;
  198.         }
  199.         else if (res)
  200.         {
  201.             m_pOptionsDlg->ShowPage(this);
  202.             return FALSE;
  203.         }
  204.     }
  205.  
  206.     return true;
  207. }
  208.  
  209. void COptionsSslPage::SaveData()
  210. {
  211.     m_pOptionsDlg->SetOption(OPTION_ENABLESSL, m_enabled ? 1 : 0);
  212.     m_pOptionsDlg->SetOption(OPTION_SSLKEYFILE, m_key);
  213.     m_pOptionsDlg->SetOption(OPTION_SSLKEYPASS, m_pass);
  214.     m_pOptionsDlg->SetOption(OPTION_SSLCERTFILE, m_certificate);
  215.     m_pOptionsDlg->SetOption(OPTION_SSLPORTS, m_sslports);
  216.     m_pOptionsDlg->SetOption(OPTION_ALLOWEXPLICITSSL, m_allowExplicit ? 1 : 0);
  217.     m_pOptionsDlg->SetOption(OPTION_SSLFORCEEXPLICIT, m_forceExplicit ? 1 : 0);
  218.     m_pOptionsDlg->SetOption(OPTION_FORCEPROTP, m_forceProtP ? 1 : 0);
  219. }
  220.  
  221. void COptionsSslPage::LoadData()
  222. {
  223.     m_enabled = m_pOptionsDlg->GetOptionVal(OPTION_ENABLESSL) != 0;
  224.     m_key = m_pOptionsDlg->GetOption(OPTION_SSLKEYFILE);
  225.     m_pass = m_pOptionsDlg->GetOption(OPTION_SSLKEYPASS);
  226.     m_certificate = m_pOptionsDlg->GetOption(OPTION_SSLCERTFILE);
  227.     m_sslports = m_pOptionsDlg->GetOption(OPTION_SSLPORTS);
  228.     m_allowExplicit = m_pOptionsDlg->GetOptionVal(OPTION_ALLOWEXPLICITSSL) != 0;
  229.     m_forceExplicit = m_pOptionsDlg->GetOptionVal(OPTION_SSLFORCEEXPLICIT) != 0;
  230.     m_forceProtP = m_pOptionsDlg->GetOptionVal(OPTION_FORCEPROTP) != 0;
  231. }
  232.  
  233. BOOL COptionsSslPage::OnInitDialog()
  234. {
  235.     COptionsPage::OnInitDialog();
  236.  
  237.     OnEnableSsl();
  238.  
  239.     return TRUE;
  240. }
  241.